﻿namespace KnownCMS.Extensions;

/// <summary>
/// 数据模型扩展类。
/// </summary>
public static class ModelExtension
{
    /// <summary>
    /// 异步添加查看日志。
    /// </summary>
    /// <param name="db">数据库对象。</param>
    /// <param name="bizType">业务类型。</param>
    /// <param name="bizId">业务ID。</param>
    /// <param name="userName">用户名。</param>
    /// <param name="ip">IP地址。</param>
    /// <returns></returns>
    public static async Task<bool> AddViewLogAsync(this Database db, string bizType, string bizId, string userName, string ip)
    {
        var log = await db.Query<CmLog>()
                          .Where(d => d.BizId == bizId && d.UserIP == ip)
                          .OrderByDescending(d => d.CreateTime)
                          .FirstAsync();
        if (log != null && log.CreateTime.ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM-dd"))
            return false;

        log = new CmLog
        {
            BizType = bizType,
            LogType = UserLogType.View.ToString(),
            BizId = bizId,
            UserId = userName,
            UserIP = ip
        };
        await db.SaveAsync(log);
        return true;
    }

    /// <summary>
    /// 将内容类别列表转换成树形结构。
    /// </summary>
    /// <param name="lists">内容类别列表。</param>
    /// <returns>树形结构。</returns>
    public static List<CmCategory> ToTreeData(this List<CmCategory> lists)
    {
        var data = new List<CmCategory>();
        var roots = lists.Where(l => l.ParentId == "0").ToList();
        foreach (var item in roots)
        {
            data.Add(item);
            AddChild(lists, item);
        }
        return data;
    }

    private static void AddChild(List<CmCategory> lists, CmCategory model)
    {
        var items = lists.Where(l => l.ParentId == model.Id).ToList();
        if (items == null || items.Count == 0)
            return;

        foreach (var item in items)
        {
            model.AddChild(item);
            AddChild(lists, item);
        }
    }

    internal static void CalculateRank(this CmPost post)
    {
        post.RankNo = (post.ViewQty ?? 0) * 1 + (post.ReplyQty ?? 0) * 2;
    }
}